package com.hongxia.assetsys.exception;

import com.hongxia.assetsys.common.BaseResponse;
import com.hongxia.assetsys.common.ErrorCode;
import com.hongxia.assetsys.common.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessExceptionHandler(BusinessException e){
        log.error("businessException:" + e.getMessage(), e);
        return ResultUtil.error(e.getCode(),e.getMessage(),e.getDescription());
    }

    @ExceptionHandler(value = AuthorizationException.class)
    public BaseResponse handleAuthenticationException(AuthorizationException e) {
        // 处理身份验证异常，可能需要返回登录页面或错误信息
        log.error("AuthenticationException:",e);
        return ResultUtil.error(ErrorCode.NO_AUTH);
    }

    @ExceptionHandler(value = UnauthenticatedException.class)
    public BaseResponse handleUnauthorizedException(UnauthenticatedException e) {
        // 处理身份验证异常，可能需要返回登录页面或错误信息
        log.error("AuthenticationException:",e);
        return ResultUtil.error(ErrorCode.NOT_LOGIN);
    }

//    @ExceptionHandler(RuntimeException.class)
//    public BaseResponse runtimeExceptionHandler(RuntimeException e){
//        log.error("runtimeException", e);
//        return ResultUtil.error(ErrorCode.SYSTEM_ERROR, e.getMessage());
//    }
}
